/*
 * File Name:               XTargetData.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2017/03/07
 */

public class XTargetData<T> 
{
    private T mCurrentData;
    private T mTargetData;

    public delegate T Lerp(T current, T target, float value);

    private XNumber mCurrentLerpValue;
    private XNumber mDeltaValuePerUpdate;
    private Lerp mHandler;

    public XTargetData(T current, T target, Lerp lerp)
    {
        mCurrentData = current;
        mTargetData = target;
        mHandler = lerp;
        mCurrentLerpValue = XNumber.zero;
    }

    public void onUpdate(float deltaTime)
    {
        if (mDeltaValuePerUpdate.raw == 0)
            return;
        else if(mDeltaValuePerUpdate.raw > 0)
        {
            if (mCurrentLerpValue >= XNumber.one)
                return;

            mCurrentLerpValue += mDeltaValuePerUpdate * 30 * deltaTime;
            if (mCurrentLerpValue > XNumber.one)
                mCurrentLerpValue = XNumber.one;
        }
        else
        {
            if (mCurrentLerpValue <= XNumber.zero)
                return;

            mCurrentLerpValue += mDeltaValuePerUpdate * 30 * deltaTime;
            if (mCurrentLerpValue < XNumber.zero)
                mCurrentLerpValue = XNumber.zero;
        }


        mCurrentData = mHandler(mCurrentData, mTargetData, mCurrentLerpValue);
    }

    public void setTargetData(T target, XNumber deltaValue)
    {
        mTargetData = target;
        mDeltaValuePerUpdate = deltaValue;
    }

    public T getCurrentData()
    {
        return mCurrentData;
    }
}
